﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain
{
    public static class ArrayUtil
    {
        /// <summary>
        /// This class will convert an array to the equivalent in C# code.
        /// Keep in mind this won't work with complex classes, like Vector3. Only with
        /// bytes, ints, shorts and stuff like that.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <returns></returns>
        public static string ConvertArrayToCSHarpCode<T>(T[] array, string name)
        {
            Type type = typeof(T);
            string typeName = type.Name;

            string code = typeName + "[] " + name + " = new " + typeName + "[" + array.Length + "] {";
            for (int i = 0; i < array.Length; i++)
            {
                code += array[i].ToString();
                if (i != array.Length - 1)
                {
                    code += ",";
                }
            }
            code += " };";

            return code;
        }

        public static Vector2[] MakeArray(Vector2 position,
           int OneDCount, Vector2 PosIncremental,
           int TwoDCount, Vector2 Two2DIncremental)
        {
            List<Vector2> Positions = new List<Vector2>();

            Vector2 startPosition = position;

            for (int i = 1; i < OneDCount + 1; i++)
            {
                //// 1D
                Vector2 d1Position = startPosition +
                    (PosIncremental * i);
                if (!Positions.Contains(d1Position))
                {
                    Positions.Add(d1Position);
                }

                for (int k = 0; k < TwoDCount; k++)
                {
                    //// 2D
                    Vector2 d2Position = d1Position +
                    (Two2DIncremental * k);
                    if (!Positions.Contains(d2Position))
                    {
                        Positions.Add(d2Position);
                    }
                }
            }

            return Positions.ToArray();
        }

        /// <summary>
        /// NEEDS OPTIMIZATION. Works, anyway
        /// </summary>
        /// <param name="position"></param>
        /// <param name="OneDCount"></param>
        /// <param name="PosIncremental"></param>
        /// <param name="TwoDCount"></param>
        /// <param name="Two2DIncremental"></param>
        /// <param name="ThreeDCount"></param>
        /// <param name="Three3DIncremental"></param>
        /// <returns></returns>
        public static Vector3[] MakeArray(Vector3 position,
            int OneDCount, Vector3 PosIncremental,
            int TwoDCount, Vector3 Two2DIncremental,
            int ThreeDCount, Vector3 Three3DIncremental)
        {
            List<Vector3> Positions = new List<Vector3>();

            Vector3 startPosition = position;

            for (int i = 1; i < OneDCount + 1; i++)
            {
                //// 1D
                Vector3 d1Position = startPosition +
                    (PosIncremental * i);
                if (!Positions.Contains(d1Position))
                {
                    Positions.Add(d1Position);
                }

                for (int k = 0; k < TwoDCount; k++)
                {
                    //// 2D
                    Vector3 d2Position = d1Position +
                    (Two2DIncremental * k);
                    if (!Positions.Contains(d2Position))
                    {
                        Positions.Add(d2Position);
                    }

                    for (int j = 1; j < ThreeDCount + 1; j++)
                    {
                        //// 3D
                        Vector3 d3Position = d2Position +
                            (Three3DIncremental * j);
                        if (!Positions.Contains(d3Position))
                        {
                            Positions.Add(d3Position);
                        }
                    }
                }
            }

            return Positions.ToArray();
        }
    }
}
